/*
 * 2005-11-16 lxm 创建。 
 */
package com.hd123.rumba.catalog;

import org.apache.axis.AxisFault;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.domain.IDomainMgr;
import com.hd123.rumba.request.SessionedService;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sql.ResultPage;

/**
 * @author lxm
 * 
 * 分类体系的Web Service对象
 */
public class DCatalogMgr extends SessionedService {

  /**
   * 取得主目录。
   * 
   * @param domainUuid
   * @param itemType
   * @return
   * @throws AxisFault
   */
  public DCatalog getMasterCatalog(String domainUuid, String itemType) throws AxisFault {
    try {
      IDomainMgr domainMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
      IDomain domain = domainMgr.getDomain(domainUuid);
      ICatalogMgr catalogMgr = (ICatalogMgr) NamingMgr.getInstance().lookupMgr(ICatalogMgr.class);
      ICatalog catalog = catalogMgr.getMasterCatalog(domain, itemType);
      DCatalog d = new DCatalog();
      d.assignFrom(catalog);
      return d;
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  /**
   * 根据uuid取得分类。
   * 
   * @param uuid
   * @return
   * @throws AxisFault
   */
  public DCategory getCategory(String uuid) throws AxisFault {
    try {
      ICatalogMgr catalogMgr = (ICatalogMgr) NamingMgr.getInstance().lookupMgr(ICatalogMgr.class);
      ICategory category = catalogMgr.getCategory(uuid);
      DCategory d = new DCategory();
      d.assignFrom(category);
      return d;
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  /**
   * 根据代码取得分类。
   * 
   * @param catalogUuid
   * @param code
   * @return
   * @throws AxisFault
   */
  public DCategory getCategoryByCode(String catalogUuid, String code) throws AxisFault {
    try {
      ICatalogMgr catalogMgr = (ICatalogMgr) NamingMgr.getInstance().lookupMgr(ICatalogMgr.class);
      ICatalog catalog = catalogMgr.getCatalog(catalogUuid);
      ICategory category = catalogMgr.getCategoryByCode(catalog, code);
      DCategory d = new DCategory();
      d.assignFrom(category);
      return d;
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  /**
   * 取得指定目录的根分类。
   * 
   * @param catalogUuid
   * @param pageSize
   * @param page
   * @return
   * @throws AxisFault
   */
  public DCategoryPage createRootCategoryPage(String catalogUuid, int pageSize, int page)
      throws AxisFault {
    try {
      ICatalogMgr catalogMgr = (ICatalogMgr) NamingMgr.getInstance().lookupMgr(ICatalogMgr.class);
      ICatalog catalog = catalogMgr.getCatalog(catalogUuid);
      ResultPage rp = catalogMgr.createCatalogCategoriesPage(catalog, 1, pageSize, page); 
      DCategoryPage drp = new DCategoryPage();
      drp.assignFrom(rp);
      return drp;
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }

  /**
   * 取得指定分类的下一级子分类。
   * 
   * @param categoryUuid
   * @param pageSize
   * @param page
   * @return
   */
  public DCategoryPage createChildCategoryPage(String categoryUuid, int pageSize, int page)
      throws AxisFault {
    try {
      ICatalogMgr catalogMgr = (ICatalogMgr) NamingMgr.getInstance().lookupMgr(ICatalogMgr.class);
      ICategory category = catalogMgr.getCategory(categoryUuid);
      ResultPage rp = catalogMgr.createChildCategoriesPage(category, 1, pageSize, page); 
      DCategoryPage drp = new DCategoryPage();
      drp.assignFrom(rp);
      return drp;
    } catch (Exception e) {
      throw AxisFault.makeFault(e);
    }
  }
}
